package cn.iocoder.yudao.module.member.controller.admin.jobmajor;

import cn.iocoder.yudao.module.member.controller.admin.jobmajor.vo.JobMajorPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.jobmajor.vo.JobMajorRespVO;
import cn.iocoder.yudao.module.member.controller.admin.jobmajor.vo.JobMajorSaveReqVO;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.dal.dataobject.jobmajor.JobMajorDO;
import cn.iocoder.yudao.module.member.service.jobmajor.JobMajorService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 岗位专业中间")
@RestController
@RequestMapping("/member/job-major")
@Validated
public class JobMajorController {

    @Resource
    private JobMajorService jobMajorService;

    @PostMapping("/create")
    @Operation(summary = "创建岗位专业中间")
    @PreAuthorize("@ss.hasPermission('wm:job-major:create')")
    public CommonResult<Long> createJobMajor(@Valid @RequestBody JobMajorSaveReqVO createReqVO) {
        return success(jobMajorService.createJobMajor(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新岗位专业中间")
    @PreAuthorize("@ss.hasPermission('wm:job-major:update')")
    public CommonResult<Boolean> updateJobMajor(@Valid @RequestBody JobMajorSaveReqVO updateReqVO) {
        jobMajorService.updateJobMajor(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除岗位专业中间")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('wm:job-major:delete')")
    public CommonResult<Boolean> deleteJobMajor(@RequestParam("id") Long id) {
        jobMajorService.deleteJobMajor(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得岗位专业中间")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('wm:job-major:query')")
    public CommonResult<JobMajorRespVO> getJobMajor(@RequestParam("id") Long id) {
        JobMajorDO jobMajor = jobMajorService.getJobMajor(id);
        return success(BeanUtils.toBean(jobMajor, JobMajorRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得岗位专业中间分页")
    @PreAuthorize("@ss.hasPermission('wm:job-major:query')")
    public CommonResult<PageResult<JobMajorRespVO>> getJobMajorPage(@Valid JobMajorPageReqVO pageReqVO) {
        PageResult<JobMajorDO> pageResult = jobMajorService.getJobMajorPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, JobMajorRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出岗位专业中间 Excel")
    @PreAuthorize("@ss.hasPermission('wm:job-major:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportJobMajorExcel(@Valid JobMajorPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<JobMajorDO> list = jobMajorService.getJobMajorPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "岗位专业中间.xls", "数据", JobMajorRespVO.class,
                        BeanUtils.toBean(list, JobMajorRespVO.class));
    }

}